<template>
  <div class="protection-management">
    <el-card>
      <el-row>
        <el-col :span="24">
          <el-button type="primary" @click="openDialog('add')">添加文章</el-button>
        </el-col>
      </el-row>
      <el-table :data="articles" style="width: 100%">
        <el-table-column prop="id" label="ID" width="50"></el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="article" label="内容"></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="openDialog('edit', scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteArticle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页功能 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="form">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input type="textarea" v-model="form.article" :rows="4"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import userApi from "@/api/userManage";
export default {
  data() {
    return {
      articles: [],
      dialogVisible: false,
      dialogTitle: '',
      searchModel:{
        pageNo:0,
        pageSize:5,
      },
      form: {
        id: null,
        title: '',
        article: '',
        image: '',
      },
    };
  },
  methods: {
    handleSizeChange(pageSize) {
      //数据更新
      this.searchModel.pageSize = pageSize;
      this.getArticleList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getArticleList();
    },
    openDialog(action, article = {}) {
      this.dialogTitle = action === 'add' ? '添加文章' : '编辑文章';
      this.form = { ...article };
      this.dialogVisible = true;
    },
    //提交新增或删除表单
    submitForm() {
      userApi.saveOrUpdataArticle(this.form).then((response) => {
        this.getArticleList();
      });
      this.dialogVisible = false;
      this.$message({
              type: 'success',
              message: '更新成功'
      })
    },
    deleteArticle(id) {
      console.log(id)
      this.$confirm('确定要删除这条记录吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      userApi.deleteArticle(id).then((response) => {
        this.getArticleList()
      });
      this.$message({
              type: 'success',
              message: '删除成功'
            })
    }).catch(() => {
      console.log('删除操作已取消');
    });
      
    },

    //用于查询用户列表
    getArticleList() {
      userApi.getArticleList(this.searchModel).then((response) => {
        console.log(response);

        this.articles = response.data.rows;
        // 将查询结果中的 total 属性赋值给 total
        this.total = response.data.total;
        console.log(this.articles);
      });
    },
  },

  //加载时就查询一次
  created() {
    this.getArticleList();
    console.log(this.bodyList);
  },
  
};
</script>

<style scoped>
.protection-management {
  padding: 20px;
}
</style> 